<?php

namespace app\admin\controller\routine;

use Throwable;
use ba\Filesystem;
use think\facade\Event;
use app\common\controller\Backend;
use app\common\model\Attachment as AttachmentModel;

class Attachment extends Backend
{
     /**
      * @var object
      * @phpstan-var AttachmentModel
      */
     protected object $model;

     protected string|array $quickSearchField = 'name';

     protected array $withJoinTable = ['admin', 'user'];

     protected string|array $defaultSortField = 'last_upload_time,desc';

     public function initialize(): void
     {
          parent::initialize();
          $this->model = new AttachmentModel();
     }


     /**
      * 查看
      * @throws Throwable
      */
     public function index(): void
     {
          $all = $this->request->param();
          if ($this->request->param('select')) {
               $this->select();
          }
          $arr = [];

          list($where, $alias, $limit) = $this->queryBuilder();
          if (isset($all['mimetype']) == 'image') {
               $arr[] = ['mimetype', 'like', 'image%'];
          }
          if (isset($all['topic']) && !empty($all['topic'])) {
               $arr[] = ['topic', '=', $all['topic']];
          }
          $res = $this->model
               ->field($this->indexField)
               ->withJoin($this->withJoinTable, $this->withJoinType)
               ->alias($alias)
               ->where($where)
               ->where($arr)
               ->order('id desc')
               ->paginate($limit);


          $this->success('', [
               'list' => $res->items(),
               'total' => $res->total(),
               'remark' => get_route_remark(),
          ]);
     }


     /**
      * 删除
      * @param array $ids
      * @throws Throwable
      */
     public function del(array $ids = []): void
     {
          if (!$this->request->isDelete() || !$ids) {
               $this->error(__('Parameter error'));
          }

          $dataLimitAdminIds = $this->getDataLimitAdminIds();
          if ($dataLimitAdminIds) {
               $this->model->where($this->dataLimitField, 'in', $dataLimitAdminIds);
          }

          $pk = $this->model->getPk();
          $data = $this->model->where($pk, 'in', $ids)->select();
          $count = 0;
          try {
               foreach ($data as $v) {
                    Event::trigger('AttachmentDel', $v);
                    $filePath = Filesystem::fsFit(public_path() . ltrim($v->url, '/'));
                    if (file_exists($filePath)) {
                         unlink($filePath);
                         Filesystem::delEmptyDir(dirname($filePath));
                    }
                    $count += $v->delete();
               }
          } catch (Throwable $e) {
               $this->error(__('%d records and files have been deleted', [$count]) . $e->getMessage());
          }
          if ($count) {
               $this->success(__('%d records and files have been deleted', [$count]));
          } else {
               $this->error(__('No rows were deleted'));
          }
     }
}
